/*
 *------------------------------------------------------------------------------
 * @File      :    ei_mai_comm_define.h
 * @Date      :    2021-4-8
 * @Author    :    lomboswer <lomboswer@lombotech.com>
 * @Brief     :    Media Interface for MADP(Media Development Platform).
 *
 * Copyright (C) 2020-2021, LomboTech Co.Ltd. All rights reserved.
 *------------------------------------------------------------------------------
 */

#ifndef __EI_MAI_COMM_DEFINE_H__
#define __EI_MAI_COMM_DEFINE_H__

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

#include "ei_type.h"
#include "ei_mai_errno.h"
#include "ei_comm_region.h"
#include "ei_comm_isp.h"

#define MUTEX_INIT_LOCK(mutex) \
do { \
	(void)pthread_mutex_init(&(mutex), NULL); \
} while(0)

#define MUTEX_LOCK(mutex) \
do { \
	(void)pthread_mutex_lock(&(mutex)); \
} while(0)

#define MUTEX_UNLOCK(mutex) \
do { \
	(void)pthread_mutex_unlock(&(mutex)); \
} while(0)

#define MUTEX_DESTROY(mutex) \
do { \
	(void)pthread_mutex_destroy(&(mutex)); \
} while(0)

#define EI_MADP_MIN_OSD_BMP_W 2
#define EI_MADP_MAX_OSD_BMP_W 4094
#define EI_MADP_MIN_OSD_BMP_H 2
#define EI_MADP_MAX_OSD_BMP_H 4094

#define EI_MADP_OSD_BMP_W_ALIGN 2
#define EI_MADP_OSD_BMP_H_ALIGN 2

#define EI_MADP_MIN_OSD_ALPHA 0
#define EI_MADP_MAX_OSD_ALPHA 255

#define EI_MADP_MIN_OSD_REGION_X 0
#define EI_MADP_MAX_OSD_REGION_X 8190
#define EI_MADP_MIN_OSD_REGION_Y 0
#define EI_MADP_MAX_OSD_REGION_Y 8190

#define EI_MADP_OSD_REGION_X_ALIGN 2
#define EI_MADP_OSD_REGION_Y_ALIGN 2

typedef struct eiMADP_RESOLUTION_S {
	EI_U32 u32Width;
	EI_U32 u32Height;
} EI_MADP_RESOLUTION_S;

typedef struct eiMADP_RECT_S {
	EI_U32 u32X;
	EI_U32 u32Y;
	EI_U32 u32Width;
	EI_U32 u32Height;
} EI_MADP_RECT_S;

typedef struct eiMADP_POINT_S {
	EI_S32 s32X;
	EI_S32 s32Y;
} EI_MADP_POINT_S;

typedef enum eiMADP_MOD_ID_E {
	EI_MADP_MOD_SYS = 0,
	EI_MADP_MOD_CAMERA,
	EI_MADP_MOD_IPPU,
	EI_MADP_MOD_VENC,
	EI_MADP_MOD_ACAP,
	EI_MADP_MOD_AENC,
	EI_MADP_MOD_ADEC,
	EI_MADP_MOD_AO,
	EI_MADP_MOD_DISP,
	EI_MADP_MOD_VDEC,
	EI_MADP_MOD_AVPLAYER,
	EI_MADP_MOD_PM,
	EI_MADP_MOD_REGION,
	EI_MADP_MOD_ISP,
	EI_MADP_MOD_BUTT,
} EI_MADP_MOD_ID_E;

typedef enum eiMADP_PIXEL_FORMAT_E {
	EI_MADP_PIXEL_FORMAT_420 = 0,
	EI_MADP_PIXEL_FORMAT_422,
	EI_MADP_PIXEL_FORMAT_RGB_1555,
	EI_MADP_PIXEL_FORMAT_RGB_8888,
	EI_MADP_PIXEL_FORMAT_RGB_BAYER_8BPP,
	EI_MADP_PIXEL_FORMAT_RGB_BAYER_10BPP,
	EI_MADP_PIXEL_FORMAT_RGB_BAYER_12BPP,
	EI_MADP_PIXEL_FORMAT_RGB_BAYER_14BPP,
	EI_MADP_PIXEL_FORMAT_RGB_BAYER,
	EI_MADP_PIXEL_FORMAT_GREY_Y8,
	EI_MADP_PIXEL_FORMAT_BUTT
} EI_MADP_PIXEL_FORMAT_E;

typedef enum eiMADP_PAYLOAD_TYPE_E {
	EI_MADP_PAYLOAD_TYPE_H264,
	EI_MADP_PAYLOAD_TYPE_H265,
	EI_MADP_PAYLOAD_TYPE_MJPEG,
	EI_MADP_PAYLOAD_TYPE_JPEG,
	EI_MADP_PAYLOAD_TYPE_AAC,
	EI_MADP_PAYLOAD_TYPE_BUTT
} EI_MADP_PAYLOAD_TYPE_E;

typedef enum eiFRAME_DATA_TYPE_E {
	EI_FRAME_DATA_TYPE_RAW,
	EI_FRAME_DATA_TYPE_YUV,
	EI_FRAME_DATA_TYPE_BUTT
} EI_FRAME_DATA_TYPE_E;

typedef enum EI_COMPRESS_MODE_E {
	EI_COMPRESS_MODE_NONE	= 0x0,
	EI_COMPRESS_MODE_SEG	= 0x1,
	EI_COMPRESS_MODE_SEG128	= 0x2,
	EI_COMPRESS_MODE_LINE	= 0x3,
	EI_COMPRESS_MODE_FRAME	= 0x4,

	EI_COMPRESS_MODE_BUTT
} EI_COMPRESS_MODE_E;

typedef struct eiFRAME_DATA_S {
    EI_FRAME_DATA_TYPE_E	enFrameDataType;
    EI_U32					u32Width;
    EI_U32					u32Height;
    EI_MADP_PIXEL_FORMAT_E	enPixelFormat;
    EI_COMPRESS_MODE_E		enCompressMode;
    EI_U32					u32PhyAddr[3];
    EI_U32 					pVirAddr[3];
    EI_U32					u32Stride[3];
	EI_U32					u32Size[3];
    EI_U64					u64pts;
    EI_U32					u32PoolId;
} EI_FRAME_DATA_S;

/* IPPU_CMD_SetPortRotate,IPPU_CMD_GetPortRotate,
EI_MADP_CAMERA_CMD_SetRotate,EI_MADP_CAMERA_CMD_GetRotate */
typedef enum eiMADP_ROTATE_E {
	EI_ROTATE_NONE = 0,
	EI_ROTATE_90 = 1,
	EI_ROTATE_180 = 2,
	EI_ROTATE_270 = 3,
	EI_ROTATE_BUTT
} EI_MADP_ROTATE_E;

typedef enum eiMADP_FLIP_MIRROR_E {
    EI_FLIP_NO,
    EI_FLIP_MIRROR,
    EI_FLIP_FLIP,
    EI_FLIP_M_AND_F,
    EI_FLIP_MAX,
} EI_MADP_FLIP_MIRROR_E;

typedef enum eiMADP_AUDIO_SAMPLE_RATE_E {
	EI_MADP_AUDIO_SAMPLE_RATE_8		= 8000,		/* 8K Sample rate     */
	EI_MADP_AUDIO_SAMPLE_RATE_11025	= 11025,	/* 11.025K Sample rate*/
	EI_MADP_AUDIO_SAMPLE_RATE_12	= 12000,	/* 12K Sample rate*/
	EI_MADP_AUDIO_SAMPLE_RATE_16	= 16000,	/* 16K Sample rate    */
	EI_MADP_AUDIO_SAMPLE_RATE_22050	= 22050,	/* 22.050K Sample rate*/
	EI_MADP_AUDIO_SAMPLE_RATE_24	= 24000,	/* 24K Sample rate    */
	EI_MADP_AUDIO_SAMPLE_RATE_32	= 32000,	/* 32K Sample rate    */
	EI_MADP_AUDIO_SAMPLE_RATE_441	= 44100,	/* 44.1K Sample rate  */
	EI_MADP_AUDIO_SAMPLE_RATE_48	= 48000,	/* 48K Sample rate    */
	EI_MADP_AUDIO_SAMPLE_RATE_64	= 64000,	/* 64K Sample rate    */
	EI_MADP_AUDIO_SAMPLE_RATE_96	= 96000,	/* 96K Sample rate    */
	EI_MADP_AUDIO_SAMPLE_RATE_BUTT
} EI_MADP_AUDIO_SAMPLE_RATE_E;

typedef enum eiMADP_AUDIO_MODE_E {
	EI_MADP_AUDIO_MODE_I2S_MASTER  = 0,		/* AIO I2S master mode */
	EI_MADP_AUDIO_MODE_I2S_SLAVE,			/* AIO I2S slave mode */
	EI_MADP_AUDIO_MODE_PCM_SLAVE_STD,		/* AIO PCM slave standard mode */
	EI_MADP_AUDIO_MODE_PCM_SLAVE_NSTD,		/* AIO PCM slave non-standard mode */
	EI_MADP_AUDIO_MODE_PCM_MASTER_STD,		/* AIO PCM master standard mode */
	EI_MADP_AUDIO_MODE_PCM_MASTER_NSTD,		/* AIO PCM master non-standard mode */
	EI_MADP_AUDIO_MODE_BUTT
} EI_MADP_AUDIO_MODE_E;

typedef enum eiMADP_AUDIO_BITWIDTH_E {
	EI_MADP_AUDIO_BITWIDTH_8   = 0,		/* Bit width is 8 bits   */
	EI_MADP_AUDIO_BITWIDTH_16  = 1,		/* Bit width is 16 bits  */
	EI_MADP_AUDIO_BITWIDTH_24  = 2,		/* Bit width is 24 bits  */
	EI_MADP_AUDIO_BITWIDTH_BUTT
} EI_MADP_AUDIO_BITWIDTH_E;

typedef enum eiMADP_AUDIO_SOUND_MODE_E {
	EI_MADP_AUDIO_SOUND_MODE_LEFT = 0,
	EI_MADP_AUDIO_SOUND_MODE_RIGHT = 1,
	EI_MADP_AUDIO_SOUND_MODE_STEREO = 2,
	EI_MADP_AUDIO_SOUND_MODE_DOUBLE_LEFT = 3,
	EI_MADP_AUDIO_SOUND_MODE_DOUBLE_RIGHT = 4,
	EI_MADP_AUDIO_SOUND_MODE_BUTT
} EI_MADP_AUDIO_SOUND_MODE_E;

typedef enum eiMADP_AUDIO_SOUND_TYPE_E {
	EI_MADP_AUDIO_SOUND_TYPE_MONO = 0,
	EI_MADP_AUDIO_SOUND_TYPE_STEREO = 1,
	EI_MADP_AUDIO_SOUND_TYPE_BUTT
} EI_MADP_AUDIO_SOUND_TYPE_E;

typedef struct eiMADP_AUDIO_GAIN_S {
	EI_S32 s32Gain;					/*Gain,0~100*/
} EI_MADP_AUDIO_GAIN_S;

typedef enum eiMADP_AUDIO_FORMAT_E {
	EI_MADP_AUDIO_FORMAT_G711A   = 1,		/* G.711 A            */
	EI_MADP_AUDIO_FORMAT_G711Mu  = 2,		/* G.711 Mu           */
	EI_MADP_AUDIO_FORMAT_ADPCM   = 3,		/* ADPCM              */
	EI_MADP_AUDIO_FORMAT_G726    = 4,		/* G.726              */
	EI_MADP_AUDIO_FORMAT_AMR     = 5,		/* AMR encoder format */
	EI_MADP_AUDIO_FORMAT_AMRDTX  = 6,		/* AMR encoder formant and VAD1 enable */
	EI_MADP_AUDIO_FORMAT_AAC     = 7,		/* AAC encoder        */
	EI_MADP_AUDIO_FORMAT_WAV     = 8,		/* WAV encoder        */
	EI_MADP_AUDIO_FORMAT_MP3     = 9,		/* MP3 encoder */
	EI_MADP_AUDIO_FORMAT_LPCM    = 10,		/* LPCM encoder */
    EI_MADP_AUDIO_FORMAT_G729    = 11,       /* G.729              */
	EI_MADP_AUDIO_FORMAT_BUTT
} EI_MADP_AUDIO_FORMAT_E;

typedef enum eiMADP_AUDIO_BITRATE_E {
	EI_MADP_AUDIO_BITRATE_16K = 0,
	EI_MADP_AUDIO_BITRATE_24K,
	EI_MADP_AUDIO_BITRATE_32K,
	EI_MADP_AUDIO_BITRATE_40K,
	EI_MADP_AUDIO_BITRATE_48K,
	EI_MADP_AUDIO_BITRATE_96K,
	EI_MADP_AUDIO_BITRATE_BUTT
} EI_MADP_AUDIO_BITRATE_E;

typedef struct eiMADP_AUDIO_FRAME_S {
	EI_MADP_AUDIO_BITWIDTH_E enBitwidth; /*audio frame bitwidth*/
	EI_MADP_AUDIO_SOUND_TYPE_E enSoundType; /*audio frame momo or stereo mode*/
	EI_VOID *pVirAddr[2];
	EI_U32 u32PhyAddr[2];
	EI_U64 u64TimeStamp;/*audio frame timestamp*/
	EI_U32 u32Seq; /*audio frame seq*/
	EI_U32 u32Len; /*data lenth per channel in frame*/
} EI_MADP_AUDIO_FRAME_S;

typedef struct eiMADP_AUDIO_FRAME_INFO_S {
	EI_VOID *pstFrame;
	EI_MADP_AUDIO_FRAME_S stAudioFrame;
	EI_U32 u32Id;
} EI_MADP_AUDIO_FRAME_INFO_S;

/** osd display attribute.dynamic attribute*/
typedef struct eiEI_MADP_OSD_DISPATTR_S {
	EI_BOOL		bShow;
	EI_U32		u32Color;
	EI_U32		u32BgColor;
	EI_U32		u32Alpha;
	EI_S32		s32RegionX;
	EI_S32		s32RegionY;
	INVERT_CORLOR_ATTR_S stInvertColorAttr;
} EI_MADP_OSD_DISPATTR_S;

/** bitmap attribute.static attribute*/
typedef struct eiEI_MADP_OSD_BITMAP_ATTR_S {
	EI_MADP_PIXEL_FORMAT_E	enPixelFormat;
	EI_U32			u32Width;
	EI_U32			u32Height;
	EI_VOID*		pData;
} EI_MADP_OSD_BITMAP_ATTR_S;

/** osd attribute*/
typedef struct eiEI_MADP_OSD_ATTR_S {
	EI_MADP_OSD_BITMAP_ATTR_S stBitmapAttr;		/**<static attribute*/
	EI_MADP_OSD_DISPATTR_S stOsdDisplayAttr;	/**<dynamic attribute*/
} EI_MADP_OSD_ATTR_S;

typedef struct eiEI_MADP_RECT_ATTR_S {
	EI_BOOL bShow;
    EI_MADP_RECT_S rect;
    EI_U32 u32Color;
    EI_U32 u32BorderSize;
    EI_U32 u32Layer;
} EI_MADP_RECT_ATTR_S;

typedef struct eiEI_MADP_MOSAIC_ATTR_S {
	EI_BOOL bShow;
    EI_MADP_RECT_S rect;
	MOSAIC_BLK_SIZE_E enBlkSize;
    EI_U32 u32Layer;
} EI_MADP_MOSAIC_ATTR_S;

typedef struct eiEI_MADP_COVER_ATTR_S {
	EI_BOOL bShow;
    EI_MADP_RECT_S rect;
	EI_U32 u32Color;
    EI_U32 u32Layer;
} EI_MADP_COVER_ATTR_S;

typedef struct eiEI_MADP_LINE_ATTR_S {
	EI_BOOL bShow;
	POINT_S stPoints[2];
	EI_U32 u32Color;
	EI_U32 u32LineSize;
	EI_U32 u32Layer;
} EI_MADP_LINE_ATTR_S;

typedef struct eiEI_MADP_ISP_EFFECT_S {
    EI_U8 u8Contrast;           /* range: [0, 255], default 128 */
    EI_U8 u8Saturation;         /* range: [0, 255], default 128 */
    EI_S8 s8Brightness;         /* range: [-128, 127], default 0 */
    EI_S8 s8Hue;                /* range: [-90, 90], default 0 */
    EI_S16 s16Sharpness;        /* range: [-1024, 1024], default 0 */
} EI_MADP_ISP_EFFECT_S;

typedef struct eiEI_MADP_ISP_FRAME_INFO_S {
	EI_U32      u32ISO;
    EI_U32      u32ExposureTime;
    EI_U32      u32IspDgain;
    EI_U32      u32Again;
    EI_U32      u32Dgain;
    EI_U32      au32Ratio[3];
    EI_S32      s32BV;
    EI_U32      u32FNumber;
    EI_U32      u32ObjLum;
    EI_U32      u32AvgLum;
    EI_U32      u32OrgTarget;
    EI_U32      u32FinalTarget;
} EI_MADP_ISP_FRAME_INFO_S;

typedef struct eiEI_MADP_EXPOSURE_S {
    EI_U32 u32AeMode;           /* range: [0, 5], 0: manual, 1, 2: auto, 3,4,5: test */
    EI_U32 u32AePriority;       /* 0: exposure by ae pline in json, 1: shutter priority, 2: gain priority, 3 aperture priority*/
    EI_U32 u32FlickerMode;      /* 0: off, 1: 50HZ, 2: 60HZ*/
    EI_FLOAT fAeSpeed;          /* range: [0.0, 1.0]*/
    EI_FLOAT fIntTime;          /* sensor exposure time, unit is s */
    EI_FLOAT fGain;             /* sensor exposure gain, unit is 1.0 */
    EI_FLOAT fHighlightWeight;  /* range: [0.1, 10], only active in linear mode, default 1.0 */
    EI_FLOAT fLowlightWeight;   /* range: [0.1, 10], only active in linear mode, default 1.0 */
    EI_S32 s32ExpBias;          /* range: -3 ~3 */
    EI_U32 u32AeTarget;         /* range: 0 ~200, when u32AeTarget is zero, ae will use target in json */
    ISP_AE_SPOT_ATTR_S stAeSpot;/*  */
} EI_MADP_EXPOSURE_S;

typedef struct eiEI_MADP_WB_ATTR_S {
    EI_U32 u32AwbMode;      /* range: [0, 9], 0: manual, 1: auto, 2: HOR, 3: A, 4: U30, 5: TL84, 6: CWF, 7: D50, 8: D65, 9: D75 */
    EI_FLOAT afWbGain[4];   /* range: [1.0, 8.0] */
} EI_MADP_WB_ATTR_S;

typedef struct eiEI_MADP_MIRROR_ATTR_S {
    EI_BOOL bEnable;
} EI_MADP_MIRROR_ATTR_S;

typedef struct eiEI_MADP_FLIP_ATTR_S {
    EI_BOOL bEnable;
} EI_MADP_FLIP_ATTR_S;

typedef EI_S32 (*PFN_CAMERA_RawDataProc)(EI_HANDLE CameraHdl, EI_FRAME_DATA_S* pCameraRawData);

#define EI_MAI_ERR_SYS_ILLEGAL_PARAM	EI_MADP_DEF_ERR(EI_MADP_MOD_SYS, MADP_EN_ERR_LEVEL_ERROR, MADP_EN_ERR_ILLEGAL_PARAM)
#define EI_MAI_ERR_SYS_NULL_PTR		EI_MADP_DEF_ERR(EI_MADP_MOD_SYS, MADP_EN_ERR_LEVEL_ERROR, MADP_EN_ERR_NULL_PTR)
#define EI_MAI_ERR_SYS_NOT_PERM		EI_MADP_DEF_ERR(EI_MADP_MOD_SYS, MADP_EN_ERR_LEVEL_ERROR, MADP_EN_ERR_NOT_PERM)
#define EI_MAI_ERR_SYS_NOTREADY		EI_MADP_DEF_ERR(EI_MADP_MOD_SYS, MADP_EN_ERR_LEVEL_ERROR, MADP_EN_ERR_SYS_NOTREADY)
#define EI_MAI_ERR_SYS_BUSY			EI_MADP_DEF_ERR(EI_MADP_MOD_SYS, MADP_EN_ERR_LEVEL_ERROR, MADP_EN_ERR_BUSY)
#define EI_MAI_ERR_SYS_NOMEM		EI_MADP_DEF_ERR(EI_MADP_MOD_SYS, MADP_EN_ERR_LEVEL_ERROR, MADP_EN_ERR_NOMEM)


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

#endif
